const app = getApp()
//获得全局js变量
var util = require('../../utils/util.js');
Page({
    data: {
        activeTab: 0,  // 0: 我的项目, 1: 下级项目
        myProjects: [],
        subordinateProjects: [],
        myTotalAmount: 0,
        subordinateTotalAmount: 0,
        isLoggedIn: false // 添加登录状态标记
    },
    
    onLoad: function() {
        this.checkLoginStatus();
    },
    
    onShow: function() {
        // 每次页面显示时检查登录状态
        this.checkLoginStatus();
    },
    
    // 检查登录状态
    checkLoginStatus: function() {
        const isLoggedIn = app.globalData.ifLogin;
        console.log('isLoggedIn:', isLoggedIn);
        this.setData({ isLoggedIn });
        
        if (isLoggedIn) {
            // 已登录，加载数据
            this.loadMockData();
        }
    },
    
    // 跳转到登录页面
    goToLogin: function() {
        wx.navigateTo({
            url: '/pages/mine/auth/auth',
        });
    },
    
    loadMockData: function() {
        var that = this;
        util.httpRrquest(app.data.rooturl + 'user/getProject', 'GET')
        .then(function (response) {
            var data = response.data.data;
            that.setData({
                myProjects: data.myProjects,
                subordinateProjects: data.subordinateProjects
             });
             // 计算总金额
            const myTotalAmount = data.myProjects.reduce((sum, project) => sum + parseFloat(project.amount || 0), 0);
            const subordinateTotalAmount = data.subordinateProjects.reduce((sum, project) => sum + parseFloat(project.amount || 0), 0);
            
            that.setData({
                myTotalAmount,
                subordinateTotalAmount
            });
        }, function (error) {
            console.log(error);
        });
        
    },
    
    switchTab: function(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            activeTab: parseInt(index)
        });
    },
    
    viewProjectDetail: function(e) {
        const projectId = e.currentTarget.dataset.id;
        // 获取当前项目数据
        let projectData = null;
        if (this.data.activeTab === 0) {
            projectData = this.data.myProjects.find(item => item.id === projectId);
        } else {
            projectData = this.data.subordinateProjects.find(item => item.id === projectId);
        }
        
        // 将项目数据存入全局数据或通过URL参数传递
        wx.navigateTo({
            url: `/pages/project-detail/project-detail?id=${projectId}`,
            success: function(res) {
                // 将数据传递给打开的页面
                res.eventChannel.emit('acceptProjectData', { project: projectData });
            },
            fail: function(err) {
                console.error('页面跳转失败:', err);
                wx.showToast({
                    title: '页面跳转失败',
                    icon: 'none'
                });
            }
        });
    },
    // 添加创建项目方法
    createProject: function() {
        wx.navigateTo({
            url: '/pages/project/create/create',
        });
    },
    // 编辑项目
    editProject: function(e) {
        //e.stopPropagation(); // 阻止冒泡，避免触发viewProjectDetail
        const projectId = e.currentTarget.dataset.id;
        const project = this.data.myProjects.find(item => item.id === projectId);
        
        wx.navigateTo({
            url: `/pages/project/edit/edit?id=${projectId}`,
            success: function(res) {
                // 将数据传递给编辑页面
                res.eventChannel.emit('acceptProjectData', { project: project });
            }
        });
    },
    
    // 管理项目成员
    manageMember: function(e) {
        //e.stopPropagation(); // 阻止冒泡，避免触发viewProjectDetail
        const projectId = e.currentTarget.dataset.id;
        
        wx.navigateTo({
            url: `/pages/project/members/members?id=${projectId}`
        });
    },
})